package com.googlecode.sc2dm.register;

import android.content.Context;
import com.google.android.c2dm.C2DMessaging;
import com.googlecode.sc2dm.annotations.AnnotationFinder;
import com.googlecode.sc2dm.annotations.OnRegistered;
import com.googlecode.sc2dm.annotations.types.OnRegisteredObject;
import com.googlecode.sc2dm.log.Logger;

import java.io.IOException;
import java.util.List;

/**
 * @Author Jarle Hansen (jarle@jarlehansen.net)
 * Created: 3:48 PM - 9/18/11
 */
public class DeviceRegistration {
    private OnRegisteredObject onRegisteredObj = null;

    public DeviceRegistration() {
    }

    public void register(Context context, String email) {
        if (context != null)
            C2DMessaging.register(context, email);
        else
            Logger.w("Context is null");
    }

    public void findAnnotations(List<Class<?>> classes) {
        for (Class clazz : classes) {
            OnRegisteredObject temp = AnnotationFinder.findOnRegistered(clazz);

            if(temp != null)
                onRegisteredObj = AnnotationFinder.findOnRegistered(clazz);
        }
    }

    public void onRegistered(Context context, String registrationId) throws IOException {
        if(onRegisteredObj != null)
            onRegisteredObj.invokeOnRegistered(context, registrationId);
    }

}
